package com.wy.boot;

import java.io.File;
import java.net.URI;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExecutableExtension;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.wizards.newresource.BasicNewProjectResourceWizard;

public class SampleWizard extends Wizard implements INewWizard, IExecutableExtension {

	private static final String PAGE_NAME = "Selenium+ Project Wizard";

	private static final String WIZARD_NAME = "New Selenium+ Project";

	private static final String WIZARD_DESCRIPTION = "Create new Selenium+ Project with all assets.";

	private SampleWizardPage _pageOne;

	private IConfigurationElement _configurationElement;

	public static String PROJECT_NAME = "SAMPLE";

	@Override
	public void addPages() {
		super.addPages();

		_pageOne = new SampleWizardPage(PAGE_NAME);
		_pageOne.setTitle(WIZARD_NAME);
		_pageOne.setDescription(WIZARD_DESCRIPTION);
		addPage(_pageOne);
	}

	public SampleWizard() {
		setWindowTitle(WIZARD_NAME);
	}

	@Override
	public void init(IWorkbench workbench, IStructuredSelection selection) {
		String selenv = System.getenv(SampleProject.SELENIUM_PLUS_ENV);
		if (selenv != null) {
			File projectdir = new File(selenv);
			if (projectdir.exists()) {
				SampleProject.SELENIUM_PLUS = selenv;
				SampleProject.STAFDIR = System.getenv(SampleProject.STAFDIR_ENV);
				return;
			}
		}
		MessageDialog.openError(getShell(), SampleProject.MSG_INSTALL_NOT_FOUND, SampleProject.MSG_INSTALL_AND_RESTART);
	}

	@Override
	public boolean performFinish() {
		String name = _pageOne.getProjectName().toUpperCase();
		URI location = null;
		if (!_pageOne.useDefaults()) {
			location = _pageOne.getLocationURI();
		} // else location == null
		SampleProject.createProject(name, location, "sas", SampleProject.PROJECTTYPE_SELENIUM);
		BasicNewProjectResourceWizard.updatePerspective(_configurationElement);
		return true;
	}

	@Override
	public void setInitializationData(IConfigurationElement config, String propertyName, Object data)
			throws CoreException {
		_configurationElement = config;
	}
}